package js;

		
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;
import org.apache.log4j.Logger;

import com.nl.sevenuser.LoadSend;
import com.nl.sevenuser.LoadSevenUser;
import com.nl.util.Common;
import com.nl.util.ConfigUtils;
import com.nl.util.ConnectionManager;

public class App 
{
	private static final Logger logger = Logger.getLogger(App.class);
	private static final String TABLE ;//七日已发表
	public static final String FLAG ;//0是正常 1是故障
	public static final String DATE_SQL ;
	public static final String MGR_IDS ;//需要清理的项目id
	public static int count1;
	public static final String REDISIP;
	public static final String NEW_DATE_SQL;
	static{
		TABLE = ConfigUtils.getInstance().getValue("table");
		FLAG = ConfigUtils.getInstance().getValue("flag");
		DATE_SQL = ConfigUtils.getInstance().getValue("dateSql");
		MGR_IDS = ConfigUtils.getInstance().getValue("mgrIds");
		REDISIP = ConfigUtils.getInstance().getValue("redisIp");
		NEW_DATE_SQL = ConfigUtils.getInstance().getValue("newDateSql");
	}
	public static void main( String[] args ) 
	{	
//		Connection conn = null;
//		ResultSet rs = null;
//		//		PreparedStatement pres = null;
//		Statement stmt = null;
//		RedisCluster redisCluster = null;
//		try {
//			redisCluster = new RedisCluster(REDISIP);
//		} catch (IOException e1) {
//			e1.printStackTrace();
//		}	
//		String sql="select receiver, mgr_id " +
//  " from boss_sm_send t " +
// " where t.mgr_id in (1000002181, 1000002182, 1000002191)" + NEW_DATE_SQL ;
//		try {			
//			conn = ConnectionManager.getInstance().getConnection();						
//			//			pres = conn.prepareStatement(sql);
//			//			pres.setString(1, cityid);
//			//			pres.setFetchSize(1000);
//			//			rs = pres.executeQuery();	
//			stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
//					ResultSet.CONCUR_READ_ONLY);
//			stmt.setFetchSize(300);
//			rs = stmt.executeQuery(sql);
//			while(rs.next()){
//				if (null ==rs.getString("receiver") || "".equals(rs.getString("receiver"))
//						|| null ==rs.getString("mgr_id") || "".equals(rs.getString("mgr_id"))
//						) {
//					continue;
//				}
//				String key = rs.getString("receiver") +"#"+rs.getString("mgr_id");
//				redisCluster.expire(key,0);
//				logger.info(key);
//			}
//		} catch (Exception e) {
//			logger.error(e.toString());
//			e.printStackTrace();
//		}
		if ("0".equals(FLAG)) {
			StringBuffer sBuffer  = new StringBuffer();
			sBuffer.append(" MERGE INTO seven_users T1  ");
			sBuffer.append(" USING (SELECT t.receiver, create_date, is_local, mgr_id, city_id, region_id  ");
			sBuffer.append("  FROM boss_sm_send t ");
			sBuffer.append("  where t.create_date > (sysdate - 1) ");
			sBuffer.append("  and mgr_id is not null ");
			sBuffer.append("  and region_id is not null) T2 ");
			sBuffer.append("  on (t1.phone_number = t2.receiver and t1.mgr_id = t2.mgr_id ) ");
			sBuffer.append("  WHEN MATCHED THEN ");
			sBuffer.append("  UPDATE SET T1.send_time = T2.create_date ");
			sBuffer.append("  WHEN NOT MATCHED THEN ");
			sBuffer.append("  INSERT ");
			sBuffer.append("    (T1.PHONE_NUMBER, ");
			sBuffer.append("     T1.ARRIVAL_CITY, ");
			sBuffer.append("     t1.send_time, ");
			sBuffer.append("     t1.region_id, ");
			sBuffer.append("     t1.mgr_id, ");
			sBuffer.append("     t1.is_local) ");
			sBuffer.append("  VALUES ");
			sBuffer.append("    (T2.receiver, ");
			sBuffer.append("     T2.city_id, ");
			sBuffer.append("     t2.create_date, ");
			sBuffer.append("    t2.region_id, ");
			sBuffer.append("     t2.mgr_id, ");
			sBuffer.append("    t2.is_local) ");
			Connection	conn1 = null;
			PreparedStatement pres1 = null;
			try {
				conn1 = ConnectionManager.getInstance().getConnection();
				pres1 = conn1.prepareStatement(sBuffer.toString());
				int result = pres1.executeUpdate();
				if (result>0) {
					logger.info("update seven_users from boss_sm_send seccess");
					Common.sendWarnSms("update seven_users from boss_sm_send seccess");
				}else {
					logger.info("update seven_users from boss_sm_send failed");
					Common.sendWarnSms("update seven_users from boss_sm_send failed");
				}
				conn1.commit();
			} catch (Exception e) {
				logger.error("update seven_users from boss_sm_send error");
			} finally {
				DbUtils.closeQuietly(conn1, pres1, null);
			}
		}
		Connection conn = null;
		ResultSet rs = null;
		ResultSet rs1 = null;
		PreparedStatement pres = null;
		int sum=0;
		int count =0;
		int avg =0;
		int num1 =0;
		int num2 =0;
		String str = "1000" ;
		StringBuffer buffer = new StringBuffer();
		buffer.append("select sum(1) as s,mgr_id from "+TABLE); 
		if ("1".equals(FLAG)) {
			buffer.append(" where ");
			buffer.append(" "+DATE_SQL);
		}
		buffer.append(" group by mgr_id order by sum(1) desc");
		String sql = buffer.toString();
		try {
			conn = ConnectionManager.getInstance().getConnection();
			pres = conn.prepareStatement(sql);
			rs = pres.executeQuery();
			while (rs.next()) {
				num1++;
				sum += Integer.parseInt(rs.getString("s")) ;				
			}
			avg = sum/25;
			rs1 = pres.executeQuery();
			while (rs1.next()) {
				num2++;
				count += Integer.parseInt(rs1.getString("s"));	
				str = str+","+rs1.getString("mgr_id");
				if (num2 == num1 ) {
					LoadSevenUser loadSevenUser = new LoadSevenUser(str);
					loadSevenUser.start();
					break;
				}
				if (count<avg) {					
					continue;
				} 				
				LoadSevenUser loadSevenUser = new LoadSevenUser(str);
				loadSevenUser.start();
				count =0;
				str = "1000";
			}
			logger.info("共加载"+count1);
			Common.sendWarnSms("加载七日已发表完毕,共加载"+count1);
		} catch (SQLException e) {			
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				rs1.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
			DbUtils.closeQuietly(conn, pres, null);

		}
		
		if ("1".equals(FLAG)) {
			logger.info("LoadSendtheard start");
			LoadSend lSend = new LoadSend();
			lSend.start();
		}
		
	}
}
